package producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class UploadToMQ {
	/**
	 * upload the msg to MQ, once by array's length
	 * @param strings
	 * @throws JMSException
	 */
	public static void upload(String[] strings) throws JMSException {
		
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://10.222.29.178:61616");
		Destination queue = new ActiveMQQueue("books");
		Connection connection = factory.createConnection();
		connection.start();
		
		Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = session.createProducer(queue);
		
		for(int i=0; i<strings.length; i++){
			TextMessage msg = session.createTextMessage(strings[i]);
			producer.send(msg);
		}

		session.commit();
		
		producer.close();
		session.close();
		connection.close();
	}

}
